package com.dianping.cat.consumer.heartbeat.model.transform;
import com.dianping.cat.consumer.heartbeat.model.IVisitor;
import com.dianping.cat.consumer.heartbeat.model.entity.Detail;
import com.dianping.cat.consumer.heartbeat.model.entity.Disk;
import com.dianping.cat.consumer.heartbeat.model.entity.Extension;
import com.dianping.cat.consumer.heartbeat.model.entity.HeartbeatReport;
import com.dianping.cat.consumer.heartbeat.model.entity.Machine;
import com.dianping.cat.consumer.heartbeat.model.entity.Period;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Map;

public class DefaultNativeParser implements IVisitor {
  private DefaultLinker m_linker;

  public DefaultNativeParser(InputStream in) {
    this.m_linker = new DefaultLinker(true);




    this.m_in = new DataInputStream(in);
  }
  private DataInputStream m_in;

  public static HeartbeatReport parse(byte[] data) { return parse(new ByteArrayInputStream(data)); }


  public static HeartbeatReport parse(InputStream in) {
/*  33 */     DefaultNativeParser parser = new DefaultNativeParser(in);
/*  34 */     HeartbeatReport heartbeatReport = new HeartbeatReport();

    try {
/*  37 */       heartbeatReport.accept(parser);
/*  38 */     } catch (RuntimeException e) {
/*  39 */       if (e.getCause() == null || !(e.getCause() instanceof java.io.EOFException))
      {

/*  42 */         throw e;
      }
    }

/*  46 */     parser.m_linker.finish();
/*  47 */     return heartbeatReport;
  }



  public void visitDetail(Detail detail) {
    byte tag;
/*  54 */     while ((tag = readTag()) != -1) {
/*  55 */       visitDetailChildren(detail, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitDetailChildren(Detail detail, int _field, int _type) {
/*  60 */     switch (_field) {
      case 1:
/*  62 */         detail.setId(readString());
        break;
      case 2:
/*  65 */         detail.setValue(readDouble());
        break;
    }
  }



  public void visitDisk(Disk disk) {
    byte tag;
/*  74 */     while ((tag = readTag()) != -1) {
/*  75 */       visitDiskChildren(disk, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitDiskChildren(Disk disk, int _field, int _type) {
/*  80 */     switch (_field) {
      case 1:
/*  82 */         disk.setPath(readString());
        break;
      case 2:
/*  85 */         disk.setTotal(readLong());
        break;
      case 3:
/*  88 */         disk.setFree(readLong());
        break;
      case 4:
/*  91 */         disk.setUsable(readLong());
        break;
    }
  }



  public void visitExtension(Extension extension) {
    byte tag;
/* 100 */     while ((tag = readTag()) != -1) {
/* 101 */       visitExtensionChildren(extension, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitExtensionChildren(Extension extension, int _field, int _type) {
/* 106 */     switch (_field) {
      case 1:
/* 108 */         extension.setId(readString());
        break;
      case 33:
/* 111 */         if (_type == 1) {
/* 112 */           Detail detail = new Detail();

/* 114 */           visitDetail(detail);
/* 115 */           this.m_linker.onDetail(extension, detail); break;
/* 116 */         }  if (_type == 2) {
/* 117 */           for (int i = readInt(); i > 0; i--) {
/* 118 */             Detail detail = new Detail();

/* 120 */             visitDetail(detail);
/* 121 */             this.m_linker.onDetail(extension, detail);
          }
        }
        break;
    }
  }



  public void visitHeartbeatReport(HeartbeatReport heartbeatReport) {
    byte tag;
/* 132 */     if ((tag = readTag()) != -4) {
/* 133 */       throw new RuntimeException(String.format("Malformed payload, expected: %s, but was: %s!", new Object[] { Integer.valueOf(-4), Byte.valueOf(tag) }));
    }

/* 136 */     while ((tag = readTag()) != -1) {
/* 137 */       visitHeartbeatReportChildren(heartbeatReport, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitHeartbeatReportChildren(HeartbeatReport heartbeatReport, int _field, int _type) {
/* 142 */     switch (_field) {
      case 1:
/* 144 */         heartbeatReport.setDomain(readString());
        break;
      case 2:
/* 147 */         heartbeatReport.setStartTime(readDate());
        break;
      case 3:
/* 150 */         heartbeatReport.setEndTime(readDate());
        break;
      case 4:
/* 153 */         if (_type == 1) {
/* 154 */           heartbeatReport.addDomain(readString()); break;
/* 155 */         }  if (_type == 2) {
/* 156 */           for (int i = readInt(); i > 0; i--) {
/* 157 */             heartbeatReport.addDomain(readString());
          }
        }
        break;
      case 5:
/* 162 */         if (_type == 1) {
/* 163 */           heartbeatReport.addIp(readString()); break;
/* 164 */         }  if (_type == 2) {
/* 165 */           for (int i = readInt(); i > 0; i--) {
/* 166 */             heartbeatReport.addIp(readString());
          }
        }
        break;
      case 33:
/* 171 */         if (_type == 1) {
/* 172 */           Machine machine = new Machine();

/* 174 */           visitMachine(machine);
/* 175 */           this.m_linker.onMachine(heartbeatReport, machine); break;
/* 176 */         }  if (_type == 2) {
/* 177 */           for (int i = readInt(); i > 0; i--) {
/* 178 */             Machine machine = new Machine();

/* 180 */             visitMachine(machine);
/* 181 */             this.m_linker.onMachine(heartbeatReport, machine);
          }
        }
        break;
    }
  }



  public void visitMachine(Machine machine) {
    byte tag;
/* 192 */     while ((tag = readTag()) != -1) {
/* 193 */       visitMachineChildren(machine, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitMachineChildren(Machine machine, int _field, int _type) {
/* 198 */     switch (_field) {
      case 1:
/* 200 */         machine.setIp(readString());
        break;
      case 2:
/* 203 */         machine.setClasspath(readString());
        break;
      case 33:
/* 206 */         if (_type == 1) {
/* 207 */           Period period = new Period();

/* 209 */           visitPeriod(period);
/* 210 */           this.m_linker.onPeriod(machine, period); break;
/* 211 */         }  if (_type == 2) {
/* 212 */           for (int i = readInt(); i > 0; i--) {
/* 213 */             Period period = new Period();

/* 215 */             visitPeriod(period);
/* 216 */             this.m_linker.onPeriod(machine, period);
          }
        }
        break;
    }
  }



  public void visitPeriod(Period period) {
    byte tag;
/* 227 */     while ((tag = readTag()) != -1)
/* 228 */       visitPeriodChildren(period, (tag & 0xFF) >> 2, tag & 0x3); 
  }
  protected void visitPeriodChildren(Period period, int _field, int _type) {
    int i;
    Map<String, String> dynamicAttribute;
/* 233 */     switch (_field) {
      case 1:
/* 235 */         period.setMinute(readInt());
        break;
      case 2:
/* 238 */         period.setThreadCount(readInt());
        break;
      case 3:
/* 241 */         period.setDaemonCount(readInt());
        break;
      case 4:
/* 244 */         period.setTotalStartedCount(readInt());
        break;
      case 5:
/* 247 */         period.setCatThreadCount(readInt());
        break;
      case 6:
/* 250 */         period.setPigeonThreadCount(readInt());
        break;
      case 7:
/* 253 */         period.setHttpThreadCount(readInt());
        break;
      case 8:
/* 256 */         period.setNewGcCount(readLong());
        break;
      case 9:
/* 259 */         period.setOldGcCount(readLong());
        break;
      case 10:
/* 262 */         period.setMemoryFree(readLong());
        break;
      case 11:
/* 265 */         period.setHeapUsage(readLong());
        break;
      case 12:
/* 268 */         period.setNoneHeapUsage(readLong());
        break;
      case 13:
/* 271 */         period.setSystemLoadAverage(readDouble());
        break;
      case 14:
/* 274 */         period.setCatMessageProduced(readLong());
        break;
      case 15:
/* 277 */         period.setCatMessageOverflow(readLong());
        break;
      case 16:
/* 280 */         period.setCatMessageSize(readDouble());
        break;
      case 33:
/* 283 */         if (_type == 1) {
/* 284 */           Disk disk = new Disk();

/* 286 */           visitDisk(disk);
/* 287 */           this.m_linker.onDisk(period, disk); break;
/* 288 */         }  if (_type == 2) {
/* 289 */           for ( i = readInt(); i > 0; i--) {
/* 290 */             Disk disk = new Disk();

/* 292 */             visitDisk(disk);
/* 293 */             this.m_linker.onDisk(period, disk);
          }
        }
        break;
      case 34:
/* 298 */         if (_type == 1) {
/* 299 */           Extension extension = new Extension();

/* 301 */           visitExtension(extension);
/* 302 */           this.m_linker.onExtension(period, extension); break;
/* 303 */         }  if (_type == 2) {
/* 304 */           for ( i = readInt(); i > 0; i--) {
/* 305 */             Extension extension = new Extension();

/* 307 */             visitExtension(extension);
/* 308 */             this.m_linker.onExtension(period, extension);
          }
        }
        break;
      case 63:
/* 313 */         dynamicAttribute = period.getDynamicAttributes();

/* 315 */         for (i = readInt(); i > 0; i--) {
/* 316 */           String key = readString();
/* 317 */           String value = readString();

/* 319 */           dynamicAttribute.put(key, value);
        }
        break;
    }
  }

  private Date readDate() {
    try {
/* 327 */       return new Date(readVarint(64));
/* 328 */     } catch (IOException e) {
/* 329 */       throw new RuntimeException(e);
    }
  }

  private double readDouble() {
    try {
/* 335 */       return Double.longBitsToDouble(readVarint(64));
/* 336 */     } catch (IOException e) {
/* 337 */       throw new RuntimeException(e);
    }
  }

  private int readInt() {
    try {
/* 343 */       return (int)readVarint(32);
/* 344 */     } catch (IOException e) {
/* 345 */       throw new RuntimeException(e);
    }
  }

  private long readLong() {
    try {
/* 351 */       return readVarint(64);
/* 352 */     } catch (IOException e) {
/* 353 */       throw new RuntimeException(e);
    }
  }

  private String readString() {
    try {
/* 359 */       return this.m_in.readUTF();
/* 360 */     } catch (IOException e) {
/* 361 */       throw new RuntimeException(e);
    }
  }

  private byte readTag() {
    try {
/* 367 */       return this.m_in.readByte();
/* 368 */     } catch (IOException e) {
/* 369 */       throw new RuntimeException(e);
    }
  }

  protected long readVarint(int length) throws IOException {
/* 374 */     int shift = 0;
/* 375 */     long result = 0L;

/* 377 */     while (shift < length) {
/* 378 */       byte b = this.m_in.readByte();
/* 379 */       result |= (b & 0x7F) << shift;
/* 380 */       if ((b & 0x80) == 0) {
/* 381 */         return result;
      }
/* 383 */       shift += 7;
    }

/* 386 */     throw new RuntimeException("Malformed variable int " + length + "!");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/heartbeat/model/transform/DefaultNativeParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */